package com.woniuxy.cloud.carl.carlgoods.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.cloud.carl.carlgoods.client.MonthSaleClient;
import com.woniuxy.cloud.carl.carlgoods.client.dto.MonthSaleDto;
import com.woniuxy.cloud.carl.carlgoods.client.param.AddSpuSalesParam;
import com.woniuxy.cloud.carl.carlgoods.infra.mapper.MonthSaleMapper;
import com.woniuxy.cloud.carl.carlgoods.infra.mapper.SkuMapper;
import com.woniuxy.cloud.carl.carlgoods.infra.model.MonthSale;
import com.woniuxy.cloud.carl.carlgoods.infra.model.Sku;
import com.woniuxy.cloud.carl.carlgoods.service.MonthSaleService;
import com.woniuxy.result.Result;
import com.woniuxy.utils.Assert;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@Slf4j
@CrossOrigin("*")
public class MonthSaleImpl implements MonthSaleClient {
    @Resource
    private SkuMapper skuMapper;
    @Resource
    private MonthSaleMapper monthSaleMapper;
@Resource
    private MonthSaleService saleService;
    @Resource
    private RedissonClient redissonClient;
    @Resource
    private SpuMannagerImpl spuMannagerImpl;
    @Override
    public Result addMothSale()  {
//        System.out.println("月销量增加");
//        RMap<Long, Integer> map = redissonClient.getMap("sku_sale:");
//        map.put(1L,10);
//        map.put(2L,20);
//        map.put(3L,30);


            //从现在开始设置上一个月是具体年月
        Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
        // calendar.add(Calendar.YEAR, -1);//当前时间减去一年，即一年前的时间
        calendar.add(Calendar.MONTH, -1);//当前时间减去一个月，即一个月前的时间
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM");
        String date = fmt.format(calendar.getTime());
        Date date1 = null;
        try {
            date1 = fmt.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        //从redis中取出每个商品月销量
        RMap<Long, Integer> skuSaleMap = redissonClient.getMap("sku_sale:");
        Set<Long> skuIds = skuSaleMap.keySet();
      List<MonthSale> monthSales = new ArrayList<>();
        for (Long skuId : skuIds) {
            Integer sales = skuSaleMap.get(skuId);
            MonthSale monthSale = new MonthSale();
            monthSale.setSales(sales);
            monthSale.setSkuId(skuId);
            monthSale.setDate(date1);
            monthSales.add(monthSale);
        }
        saleService.saveBatch(monthSales);//插入月销表
        AddSpuSalesParam addSpuSalesParam = new AddSpuSalesParam();
        addSpuSalesParam.setMap(skuSaleMap);
        spuMannagerImpl.addSpuSales(addSpuSalesParam);//增加sku商品销量同时增加商品类别spu销量
        skuSaleMap.delete();

        return Result.success();
    }

    @Override
    public Result<List<MonthSaleDto>> allMonthSale() {
        QueryWrapper<MonthSale> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("sales");
        List<MonthSale> monthSales = monthSaleMapper.selectList(queryWrapper);
        List<MonthSaleDto> monthSaleDtos = BeanUtil.copyToList(monthSales, MonthSaleDto.class);
        for (MonthSaleDto monthSaleDto : monthSaleDtos) {
            Sku sku = skuMapper.selectById(monthSaleDto.getSkuId());
            if(sku!=null) {
                monthSaleDto.setSkuName(sku.getSkuName());
            }
        }

        return Result.success(monthSaleDtos);
    }

    @Override
    public Result<List<MonthSaleDto>> findMonthSaleByskuId(Integer skuId) {
        Sku sku = skuMapper.selectById(skuId);
        Assert.state(sku!=null,"该种商品不存在");
        QueryWrapper<MonthSale> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sku_id",skuId);
        queryWrapper.orderByDesc("date");
        List<MonthSale> monthSales = monthSaleMapper.selectList(queryWrapper);
        List<MonthSaleDto> monthSaleDtos = BeanUtil.copyToList(monthSales, MonthSaleDto.class);
        for (MonthSaleDto monthSaleDto : monthSaleDtos) {
            Sku sku1 = skuMapper.selectById(monthSaleDto.getSkuId());
            if(sku1!=null) {
                monthSaleDto.setSkuName(sku.getSkuName());
            }
        }
        if(monthSaleDtos==null||monthSaleDtos.size()==0){
            MonthSaleDto monthSaleDto = new MonthSaleDto();

            monthSaleDto.setSkuId(Long.valueOf(skuId));

            monthSaleDto.setSales(0);
            monthSaleDtos.add(monthSaleDto);

        }


        return Result.success(monthSaleDtos);
    }

//    @Scheduled(cron = "*/1 * * * * ?")
//    public static void FoodVariety(){
//        System.out.println(LocalDateTime.now()+"定时任务");
//    }
}
